package com.nbsaas.boot.project.ext.resource;

import com.nbsaas.boot.project.api.apis.ProjectApi;
import com.nbsaas.boot.project.api.apis.ProjectMemberApi;
import com.nbsaas.boot.project.api.domain.request.ProjectMemberRequest;
import com.nbsaas.boot.project.api.domain.response.ProjectResponse;
import com.nbsaas.boot.project.ext.apis.ProjectExtApi;
import com.nbsaas.boot.project.ext.domain.request.ProjectExtRequest;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ProjectExtResource implements ProjectExtApi {

    @Resource
    private ProjectApi projectApi;

    @Resource
    private ProjectMemberApi projectMemberApi;

    @Transactional
    @Override
    public ResponseObject<?> createExt(ProjectExtRequest request) {
        ResponseObject<ProjectResponse> result = new ResponseObject<>();
        ProjectResponse res = projectApi.createData(request);
        if (request.getMembers() != null) {
            for (Long member : request.getMembers()) {
                ProjectMemberRequest memberReq = new ProjectMemberRequest();
                memberReq.setProject(res.getId());
                memberReq.setMember(member);
                projectMemberApi.createData(memberReq);
            }
        }
        return result;
    }
}
